package com.hupun.service;

import java.util.Collection;
import java.util.Set;
import java.util.UUID;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.annotation.DataResolver;
import com.bstek.dorado.data.entity.EntityState;
import com.bstek.dorado.data.entity.EntityUtils;
import com.hupun.mybatis.mapper.ProductsClassificationMapper;
import com.hupun.mybatis.mapper.ProductsMapper;
import com.hupun.mybatis.pojo.ProductsClassification;

@Component
public class MenuService {
	@Resource(name = "productsClassificationMapper")
	private ProductsClassificationMapper productsClassificationMapper;
	@Resource
	private ProductsMapper productsMapper;

	@DataProvider
	public Collection<ProductsClassification> getTopMenu() {
		Collection<ProductsClassification> list = productsClassificationMapper
				.selectByParentId();
		return list;
	}

	@DataProvider
	public Collection<ProductsClassification> getChilds(String productId) {
		Collection<ProductsClassification> list = productsClassificationMapper
				.getChilds(productId);
		return list;
	}
	
	@DataResolver
	@Transactional
	public void addClass(Collection<ProductsClassification> list){
		for (ProductsClassification productsClassification : list) {
			if(EntityState.NEW.equals(EntityUtils.getState(productsClassification))){
				String randomUUID = UUID.randomUUID().toString();
				randomUUID = randomUUID.replaceAll("-", "").substring(0, 9);
				productsClassification.setProductId(randomUUID);
				productsClassificationMapper.insertClass(productsClassification);
			}else if(EntityState.MODIFIED.equals(EntityUtils.getState(productsClassification))){
				productsClassificationMapper.updateClass(productsClassification);
			}else if(EntityState.DELETED.equals(EntityUtils.getState(productsClassification))){
				String productId = productsClassification.getProductId();
				productsClassificationMapper.deletedClass(productId);
				String topId = "01";
				productsMapper.updateByClassId(productId,topId);
			}
		}
	}
	@DataResolver
	public void saveClass(Collection<ProductsClassification> list){
		for (ProductsClassification productsClassification : list) {
			
			Set<ProductsClassification> productsClassificationSet = productsClassification.getProductsClassificationSet();
			
			for (ProductsClassification productsClass : productsClassificationSet) {
				
				if(EntityState.NEW.equals(EntityUtils.getState(productsClass))){
					
					String randomUUID = UUID.randomUUID().toString();
					randomUUID = randomUUID.replaceAll("-", "").substring(0, 9);
					productsClassification.setProductId(randomUUID);
					productsClassificationMapper.insertClass(productsClass);
					
				}else if(EntityState.MODIFIED.equals(EntityUtils.getState(productsClass))){
					
					productsClassificationMapper.updateClass(productsClass);
				}else if(EntityState.DELETED.equals(EntityUtils.getState(productsClass))){
					
					String productId = productsClass.getProductId();
					productsClassificationMapper.deletedClass(productId);
					//String topId = "01";
					//productsMapper.updateByClassId(productId,topId);
				}
			}
		}
	}
	
	@DataProvider
	public Collection<ProductsClassification> getTree(String productId){
		return productsClassificationMapper.getChilds(productId);
	}
}
